package program;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import stock.ActionType;
import stock.BackwardChainingStockRules;
import stock.Company;
import stock.ScreenScrapper;


public class BackwardChaining extends ChainingInference{

	public BackwardChaining() {
		
		final JFrame frame = new JFrame("Stock - Backward Chaining");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(GUIConstants.frameWidth,GUIConstants.frameHeight));
		frame.setLocationRelativeTo(null);
		
		final JLabel actionLabel = new JLabel("Choose Action Type:");
		final JPanel actionIdealPanel = new JPanel();
		JScrollPane scrollIdealActionPanel = new JScrollPane(actionIdealPanel);
		actionIdealPanel.setBackground(GUIConstants.whiteColor);
		actionIdealPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		
		final JComboBox<ActionType> comboActions = new JComboBox<>(ActionType.values());
		
		JButton buttonLookUp = new JButton("Look Up");
		buttonLookUp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<Company> companies = ScreenScrapper.gatherCompaniesInformation();
				Company merval = ScreenScrapper.gatherMervalInformation();
				
				if ( (companies != null) && (merval != null) ){

					BackwardChainingStockRules backwardProcessing = new BackwardChainingStockRules(merval, companies, 500, (ActionType) comboActions.getSelectedItem());
					
					setInformation(actionIdealPanel, backwardProcessing.getCompaniesActionTypeIdeal(), merval);
					
				}

			}
		});
		
		JPanel panelButtons = new JPanel();
		panelButtons.add(actionLabel);
		panelButtons.add(comboActions);
		panelButtons.add(buttonLookUp);
		
		frame.getContentPane().add(panelButtons, BorderLayout.PAGE_START);
		frame.getContentPane().add(scrollIdealActionPanel, BorderLayout.CENTER);
		frame.getContentPane().add(Box.createRigidArea(new Dimension(GUIConstants.frameWidth/12,0)), BorderLayout.LINE_START);
		frame.getContentPane().add(Box.createRigidArea(new Dimension(GUIConstants.frameWidth/12,0)), BorderLayout.LINE_END);
		frame.getContentPane().add(Box.createRigidArea(new Dimension(0,GUIConstants.frameHeight/12)), BorderLayout.PAGE_END);
		
		frame.setVisible(true);	
		
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					new BackwardChaining();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}
}
